﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.DataAccess;
using Gasco.Business.Services;
using Mani.Common;

public partial class Controls_loading_add : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ProductService productService = new ProductService();
            LookupService<transportation_type> lookupService = new LookupService<transportation_type>();

            ddTransportationType.DataSource = lookupService.GetAll();
            ddTransportationType.DataBind();

        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            if (!Page.IsPostBack)
            {
                LoadingService service = new LoadingService();
                loading l = service.GetLoading(Convert.ToInt32(Request.QueryString["id"]));
                OrderService os = new OrderService();
                ContactService cs = new ContactService();
                tbOrder.Text = os.GetOrderById(l.order_id).order_name;
                tbOrderNum.Text = os.GetOrderById(l.order_id).order_number;
                tbQuantity.Text = l.quantity.Round(3).ToString();
                ddTransportationType.SelectedValue = l.transportation_type_id.ToString();
                tbCmrNumber.Text = l.cmr_number.ToString();
                tbLoadingDate.Text = l.loading_date.ToJalali();
                tbVesseNumber.Text = l.vessel_number.ToString();
                tbTransporterPerson.Text = cs.GetContactById(Convert.ToInt32(l.transporter_person_id)).name.ToString();
                tbLoadingCustomDate.Text = l.loading_custom_date.ToJalali();
                tbBorderArrivalDate.Text = l.border_arrival_date.ToJalali();
                tbBorderDepartureDate.Text = l.border_departure_date.ToJalali();
                tbOffLoadingDate.Text = l.offloading_date.ToJalali();
                
                float remainingQuantityBeforeLoading = service.RemainLoadingByOrderNum(tbOrderNum.Text, l.id);
                tbCalcOrderQuantity.Text = remainingQuantityBeforeLoading.Round(3).ToString() ;
                tbCalcRemind.Text = (remainingQuantityBeforeLoading - l.quantity).Round(3).ToString();
                tbOffLoadQuantity.Text = l.offload_quantity.Round(3).ToString();
            }
        }
    }

    protected void btnclose_Click(object sender, EventArgs e)
    {
        InsertUpdateInfo();
    }

    private void InsertUpdateInfo()
    {
        bool InsertMode = (Request.QueryString["id"] == null);
        LoadingService service = new LoadingService();
        OrderService os = new OrderService();
        ContactService cs = new ContactService();
        loading l;

        if (InsertMode)
        {
            l = new loading();
        }
        else
        {
            l = service.GetLoading(Convert.ToInt32(Request.QueryString["ID"]));
        }

        

        try
        {
            l.transporter_person_id = cs.GetContactIdByName(tbTransporterPerson.Text);
        }
        catch (ContactNameNotFoundException ex)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("ContactNameNotFoundException") + ex.ContactName);
            return;
        }

        try 
        {
        if (!String.IsNullOrEmpty(tbOrder.Text))
            l.order_id = os.GetOrderByName(tbOrder.Text).id;
        }
        catch(OrderNameNotFoundException ex)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("OrderNameNotFoundException")+ex.OrderName);
            return;
        }

        try 
        {
            if (!String.IsNullOrEmpty(tbOrderNum.Text))
                l.order_id = os.GetOrderByNumber(tbOrderNum.Text).id;
        }
        catch(OrderNumberNotFoundException ex)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("OrderNumberNotFoundException") + ex.OrderNumber);
            return;
        }

        
       
        OrderService orderService = new OrderService();

        
        l.transportation_type_id = Convert.ToInt32(ddTransportationType.SelectedValue);
        l.quantity = Convert.ToSingle(tbQuantity.Text).Round(3);
        l.cmr_number = tbCmrNumber.Text;
        
        if(!String.IsNullOrEmpty(tbOffLoadQuantity.Text)) 
           l.offload_quantity = Convert.ToSingle(tbOffLoadQuantity.Text).Round(3);
        
        l.vessel_number = tbVesseNumber.Text;

        if (!String.IsNullOrEmpty(tbLoadingDate.Text))
            l.loading_date = tbLoadingDate.Text.ToDateTime();
        else
            l.loading_date = null;


        if(!String.IsNullOrEmpty(tbLoadingCustomDate.Text))
            l.loading_custom_date = tbLoadingCustomDate.Text.ToDateTime();
        else
            l.loading_custom_date = null;

        if(!String.IsNullOrEmpty(tbBorderArrivalDate.Text))
            l.border_arrival_date = tbBorderArrivalDate.Text.ToDateTime();
        else
            l.border_arrival_date = null;

        if(!String.IsNullOrEmpty(tbBorderDepartureDate.Text))
            l.border_departure_date = tbBorderDepartureDate.Text.ToDateTime();
        else
            l.border_departure_date = null;
        
        if(!String.IsNullOrEmpty(tbOffLoadingDate.Text))
            l.offloading_date = tbOffLoadingDate.Text.ToDateTime();
        else
            l.offloading_date = null;


        try
        {
            if (InsertMode)
                service.CreateLoading(l);
            else
                service.UpdateLoading(l);

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
        }
        catch (BeforeDateEmpty)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("BeforDateEmpty").ToString());
        }
        catch (DateMismatch)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("DateMismatch").ToString());
        }
        catch (TotalLoadingQuantityMustEqualOrLessThanOrder)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("TotalLoadingQuantityMustEqualOrLessThanOrder").ToString());
        }
        catch (DuplicateCmrNumber)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("DuplicateCrmNumber").ToString());
        }
        catch (LoadingDatelessThanOrderStartDate)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("LoadingDatelessThanOrderStartDate").ToString());
        }
        catch (InvalidOrderStatusException)
        {
            UIHelper.ClientAlert(this.Page, GetLocalResourceObject("InvalidOrderStatusException").ToString());
        }

    }
}
